home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-07-18 | 10.6 KB | 447 lines |
- /*
- * @(#)NSRequest.java 1.11 97/05/22
- *
- * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
- *
- * This software is the confidential and proprietary information of Sun
- * Microsystems, Inc. ("Confidential Information"). You shall not
- * disclose such Confidential Information and shall use it only in
- * accordance with the terms of the license agreement you entered into
- * with Sun.
- *
- * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
- * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
- * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
- * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
- * THIS SOFTWARE OR ITS DERIVATIVES.
- *
- * CopyrightVersion 1.0
- */
-
- package sun.servlet.netscape;
-
- import java.io.IOException;
- import java.util.Dictionary;
- import java.util.Hashtable;
- import java.util.Enumeration;
- import java.util.Date;
- import java.util.StringTokenizer;
- import javax.servlet.ServletInputStream;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpUtils;
- import sun.servlet.http.HttpInputStream;
- import netscape.server.applet.URIUtil;
-
- /**
- * This class represents a servlet request for servlets running in Netscape's
- * Enterprise or FastTrack servers. The request object is passed to the servlet
- * by a special Netscape "server applet".
- *
- * @version 1.11, 05/22/97
- * @author David Connelly
- */
- public
- class NSRequest implements HttpServletRequest {
- /*
- * The server applet for running servlets.
- */
- private NSRunner sr;
-
- /*
- * The servlet path for this request.
- */
- String servletPath;
-
- /*
- * The path info for this request.
- */
- String pathInfo;
-
- /*
- * The query string for this request.
- */
- private String queryString;
-
- /*
- * The query parameters for this request.
- */
- private Dictionary params = null;
-
- /*
- * The servlet input stream for the request.
- */
- private HttpInputStream in = new HttpInputStream();
-
- /*
- * The headers for this request.
- */
- private Hashtable headers;
-
- /*
- * Initializes the servlet request.
- */
- void init(NSRunner sr) throws IOException {
- this.sr = sr;
- in.init(sr.getInputStream());
- in.next();
- int len = getContentLength();
- if (len != -1) {
- in.setContentLength(len);
- }
- queryString = sr.getRequestProperty("query");
- /*
- if (queryString != null) {
- params = URIUtil.splitFormData(queryString);
- } else {
- params = new Hashtable();
- }
- */
- }
-
- /*
- * Resets the request.
- */
- void reset() {
- sr = null;
- servletPath = null;
- pathInfo = null;
- queryString = null;
- params = null;
- in.resets();
- headers = null;
- }
-
- /**
- * Returns the content length of the request.
- */
- public int getContentLength() {
- String v = sr.getHeader("content-length");
- try {
- return v != null ? Integer.parseInt(v) : -1;
- } catch (NumberFormatException e) {
- return -1;
- }
- }
-
- /**
- * Returns the content type of the request.
- */
- public String getContentType() {
- return sr.getHeader("content-type");
- }
-
- /**
- * Returns the protocol for the request.
- */
- public String getProtocol() {
- return sr.getRequestProperty("protocol");
- }
-
- /**
- * Returns the scheme of the request -- always "http" for now.
- */
- public String getScheme() {
- return "http"; // XXX might be "https" too!
- }
-
- /**
- * Returns the host name of the server.
- */
- public String getServerName() {
- return sr.getServer().getAddress().getHostName();
- }
-
- /**
- * Returns the port number on which this request was received.
- */
- public int getServerPort() {
- return sr.getServer().getListeningPort();
- }
-
- /**
- * Returns the client host address.
- */
- public String getRemoteAddr() {
- return sr.getClientProperty("ip");
- }
-
- /**
- * Returns the client host name.
- */
- public String getRemoteHost() {
- return sr.getClientProperty("dns");
- }
-
- /**
- * Returns an input stream for reading the request.
- * @exception IOException if an I/O error has occurred
- */
- public ServletInputStream getInputStream() {
- return in;
- }
-
- /**
- * Returns an array of values for the specified query parameter.
- */
- public String [] getParameterValues(String name) {
-
- if (params == null)
- params = getParametersFromRequest();
-
- return (String []) params.get(name);
- }
-
- /**
- * Returns the value of the specified query parameter.
- */
- public String getParameter(String name) {
-
- if (params == null)
- params = getParametersFromRequest();
-
- String vals[] = (String []) params.get(name);
-
- if (vals == null)
- return null;
-
- String hackVal = vals[0];
- for (int i = 1; i < vals.length; i++)
- hackVal = hackVal + "," + vals[i];
-
- return hackVal;
-
- }
-
- /**
- * Returns an enumeration of query parameter names.
- */
- public Enumeration getParameterNames() {
-
- if (params == null)
- params = getParametersFromRequest();
-
- return params.keys();
- }
-
- /**
- * Returns the request method.
- */
- public String getMethod() {
- return sr.getRequestProperty("method");
- }
-
- /**
- * Returns the request URI.
- */
- public String getRequestURI() {
- String file = sr.getRequestProperty("uri");
- if (queryString != null) {
- file = file + "?" + queryString;
- }
- return file;
- }
-
- /**
- * Returns the path info part of the request URI.
- */
- public String getPathInfo() {
- return pathInfo;
- }
-
- /**
- * Returns the servlet path.
- */
- public String getServletPath() {
- return servletPath;
- }
-
- /**
- * Returns path info translated.
- */
- public String getPathTranslated() {
- return pathInfo != null ? sr.translateURI(pathInfo) : null;
- }
-
- /**
- * Returns corresponding real path for specified virtual path.
- */
- public String getRealPath(String path) {
- return sr.translateURI(path);
- }
-
- /**
- * Returns the query string for the request.
- */
- public String getQueryString() {
- return queryString;
- }
-
- /**
- * Returns the authenticated user if any.
- */
- public String getRemoteUser() {
- return sr.getServerProperty("auth-user");
- }
-
- /**
- * Returns the authorization type for the request.
- */
- public String getAuthType() {
- return sr.getServerProperty("auth-type");
- }
-
- /**
- * Returns the value of the header field with the specified name.
- * @param name the header field name
- * @return the header field value
- */
- public String getHeader(String name) {
- return sr.getHeader(name.toLowerCase());
- }
-
- /*
- * Returns the integer value of the header field with the specified name.
- * @param name the header field name
- * @return the header field integer value, or -1 if not found
- */
- public int getIntHeader(String name) {
- String v = getHeader(name);
- try {
- return v != null ? Integer.parseInt(v) : -1;
- } catch (NumberFormatException e) {
- return -1;
- }
- }
-
- /*
- * Returns the date value of the header field with the specified name.
- * @param name the header field name
- * @return the header field date value, or -1 if not found
- */
- public long getDateHeader(String name) {
- String v = getHeader(name);
- try {
- return v != null ? Date.parse(v) : -1;
- } catch (IllegalArgumentException e) {
- return -1;
- }
- }
-
- /**
- * Returns an enumeration of header names for this request.
- */
- public Enumeration getHeaderNames() {
- if (headers == null) {
- headers = new Hashtable(HEADERS.length);
- for (int i = 0; i < HEADERS.length; i++) {
- String name = HEADERS[i];
- String value = sr.getHeader(name.toLowerCase());
- if (value != null) {
- headers.put(name, value);
- }
- }
- }
- return headers.keys();
- }
-
- /**
- * Returns an attribute of the request for the specified key name.
- * @param name the attribute name
- */
- public Object getAttribute(String name) {
- return null;
- }
-
- /*
- * Possible request header field names.
- */
- private static String[] HEADERS = {
- /* Standard HTTP 1.0 and 1.1 headers */
- "Accept", "Accept-Charset", "Accept-Encoding",
- "Accept-Language", "Accept-Ranges", "Age",
- "Allow", "Authorization", "Cache-Control",
- "Connection", "Content-Base", "Content-Encoding",
- "Content-Language", "Content-Length", "Content-Location",
- "Content-MD5", "Content-Range", "Content-Type",
- "Date", "ETag", "Expires",
- "From", "Host", "If-Modified-Since",
- "If-Match", "If-None-Match", "If-Range",
- "If-Unmodified-Since", "Last-Modified", "Location",
- "Max-Forwards", "Pragma", "Proxy-Authenticate",
- "Proxy-Authorization", "Public", "Range",
- "Referer", "Retry-After", "Server",
- "Transfer-Encoding", "Upgrade", "User-Agent",
- "Vary", "Via", "Warning",
- "WWW-Authenticate",
- /* Popular extension headers */
- "Alternates", "Content-Version", "Cookie",
- "Derived-From", "Keep-Alive", "Link",
- "MIME-Version", "URI", "Title"
- };
-
- /**
- * decode a URLencoded string, so we may use it as a string.
- */
- private String decode(String encoded) {
-
-
- //speedily leave if we're not needed
- if (encoded == null) return null;
- if (encoded.indexOf('%') == -1 ) return encoded;
-
- //allocate the buffer - use byte[] to avoid calls to new.
- byte holdbuffer[] = new byte[encoded.length()];
-
- char holdchar;
- int bufcount = 0;
-
- for (int count = 0; count < encoded.length(); count++) {
- if (encoded.charAt(count) == '%') {
- holdbuffer[bufcount++] =
- (byte)Integer.parseInt(encoded.substring(count+1,count+3),16);
- if (count + 2 >= encoded.length())
- count = encoded.length();
- else
- count += 2;
- } else {
- holdbuffer[bufcount++] = (byte)encoded.charAt(count);
- }
- }
- return new String(holdbuffer,0,0,bufcount);
- }
-
- private static Hashtable nullHashtable = new Hashtable();
-
-
- private Hashtable getParametersFromRequest() {
-
- Hashtable h = null;
-
- if (getMethod().equals("GET")) {
- String s = getQueryString();
- if (s != null) {
- try {
- h = HttpUtils.parseQueryString(s);
- } catch (IllegalArgumentException e) {
- h = null;
- }
- }
- } else if (getMethod().equals("POST")) {
- if (getContentType().equals("application/x-www-form-urlencoded")) {
- try {
- h = HttpUtils.parsePostData(getContentLength(),
- getInputStream());
- } catch (Exception e) {
- h = null;
- }
- }
- }
-
- if (h == null)
- h = nullHashtable;
-
- return h;
- }
- }
-